﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class Account_ChangePassword : System.Web.UI.Page
{

    protected void Page_Load(Object sender, EventArgs e)
    {
        //Manually register the event-handling methods.
        ChangeUserPassword.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
    }

    protected void _ChangingPassword(Object sender, LoginCancelEventArgs e)
    {
        string currentPassword = ChangeUserPassword.CurrentPassword.ToString();
        string newPassword = ChangeUserPassword.NewPassword.ToString();
        Boolean passwordChanged = false;
     
        if (newPassword.Length > 5)
        {
            passwordChanged = changePassword(ChangeUserPassword.UserName.ToString(), currentPassword, newPassword);
            if (passwordChanged)
            {
                e.Cancel = true;
                Response.Redirect("~/Account/ChangePasswordSuccess.aspx");
            }
            else
            {
                Debug.WriteLine(":((");
            }
        }

    }

    private Boolean changePassword(string username, string currentPassword, string newPassword)
    {
        JsonParser jsonParser = new JsonParser();
        Boolean passwordChanged = jsonParser.changePassword(username, currentPassword, newPassword);
        return passwordChanged;
    }

}
